﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class PhanQuyenDAO
    {
        public List<PhanQuyenDTO> LayDanhSachPhanQuyen(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachPhanQuyen", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<PhanQuyenDTO> list = new List<PhanQuyenDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    PhanQuyenDTO phanQuyen = new PhanQuyenDTO();
                    phanQuyen.MaQuyenHan = dt.Rows[i]["MQH"].ToString();
                    phanQuyen.TenQH = dt.Rows[i]["TenQH"].ToString();
                    phanQuyen.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    list.Add(phanQuyen);
                }
            }
            return list;
        }
        public string ThemPhanQuyen(PhanQuyenDTO phanQuyen, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemPhanQuyen", new SqlParameter("@tenPQ", phanQuyen.TenQH), par);
                                                           

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaPhanQuyen(PhanQuyenDTO phanQuyen, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaPhanQuyen", new SqlParameter("@maPQ", phanQuyen.MaQuyenHan),
                                                            new SqlParameter("@tenPQ", phanQuyen.TenQH),
                                                             new SqlParameter("@daXoa", phanQuyen.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaPhanQuyen(PhanQuyenDTO phanQuyen, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaPhanQuyen", new SqlParameter("@maPQ", phanQuyen.MaQuyenHan), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}